package com.zero.clinic.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zero.clinic.pojo.*;
import com.zero.clinic.service.PermissionService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/permission")
public class PermissionController {
    @Resource
    private PermissionService permissionService;

    /**
     * @function 进入权限管理模块
     * @return
     */
    @RequiresPermissions("permission:permissionPage")
    @RequestMapping("/permissionPage")
    public String permissionPage(){
        return "permissionPage";
    }

    /**
     * @function 权限列表
     * @param pageNum
     * @param pageSize
     * @param keywords
     * @return
     */
    @ResponseBody
    @RequiresPermissions("permission:list")
    @RequestMapping("/list")
    public PageInfo<Permission> permissionList(@RequestParam(defaultValue = "1") int pageNum,
                                   @RequestParam(defaultValue = "10") int pageSize, String keywords){
        PageHelper.startPage(pageNum, pageSize);
        // 1.查出所有的Permission信息
        PermissionExample example = new PermissionExample();
        // 2 根据搜索关键词查询
        if (StringUtils.isNotBlank(keywords)){
            // 2.1 根据子权限名称查询
            PermissionExample.Criteria criteria= example.createCriteria();
            criteria.andPermissionNameLike("%"+ keywords +"%");
            // 2.2 根据父权限名称查询
            PermissionExample.Criteria criteria2 = example.or();
            criteria2.andParentNameLike("%" + keywords + "%");
        }
        List<Permission> permissions = permissionService.selectByExample(example);
        // 3.以PageInfo对象返回给前端
        PageInfo<Permission> pageInfo = new PageInfo<>(permissions);
        return pageInfo;
    }

    /**
     * @function 编辑权限页面
     * @param permissionId
     * @param m
     * @return
     */
    @RequestMapping("/edit")
    public String permissionEdit(Long permissionId, Model m){

        // 1.如果前端页面需要更新权限信息，则会传一个需要更新的permissionId过来。这里需要通过permissionId查出更新信息后共享给前端页面
        if (permissionId != null) {
            Permission permission = permissionService.selectByPrimaryKey(permissionId);
            m.addAttribute("permission",permission);
        }

        // 2.将所有menu类型的权限作为父权限共享给前端页面
        PermissionExample example = new PermissionExample();
        PermissionExample.Criteria criteria = example.createCriteria();
        criteria.andPermissionTypeEqualTo("menu");
        List<Permission> parentPermissions = permissionService.selectByExample(example);
        m.addAttribute("parentPermissions", parentPermissions);

        return "permissionEdit";
    }

    /**
     * @function 新增权限
     * @param permission
     * @return
     */
    @ResponseBody
    @RequestMapping("/insert")
    @RequiresPermissions("permission:insert")
    public MessageObject permissionInsert(Permission permission){
        //1.插入数据
        System.out.println("需要新增的权限：" + permission);
        int row = permissionService.insert(permission);
        if (row > 0) {
            return MessageObject.createMo(1,"新增数据成功");
        }
        return MessageObject.createMo(0,"新增数据失败，请联系技术人员");
    }

    /**
     * @function 更新权限
     * @param permission
     * @return
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("permission:update")
    public MessageObject permissionUpdate(Permission permission){
        //1.更新数据
        System.out.println("需要更新的权限" + permission);
        int row = permissionService.updateByPrimaryKeySelective(permission);
        if (row > 0) {
            return MessageObject.createMo(1,"更新数据成功");
        }
        return MessageObject.createMo(0,"更新数据失败，请联系技术人员");
    }

    /**
     * @function 检查权限名称是否重复
     * @param permissionName
     * @return
     */
    @ResponseBody
    @RequestMapping("/checkPermissionName")
    @RequiresPermissions("permission:checkPermissionName")
    public boolean permissionCheckPermissionName(String permissionName){
        PermissionExample example = new PermissionExample();
        PermissionExample.Criteria criteria = example.createCriteria();
        criteria.andPermissionNameEqualTo(permissionName);
        List<Permission> permissions = permissionService.selectByExample(example);
        return permissions.size() > 0 ? false : true;
    }

    /**
     * @function 删除权限
     * @param permissionId
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete")
    @RequiresPermissions("permission:delete")
    public MessageObject permissionDelete(Long permissionId){
        //1.删除数据前判断该权限下是否还有子权限，如果有则不能删除
        PermissionExample example = new PermissionExample();
        PermissionExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(permissionId);
        List<Permission> permissions = permissionService.selectByExample(example);
        if (permissions.size() > 0) {
            return MessageObject.createMo(0, "该权限下还有子权限，不能删除");
        }

        //2.根据permissionId删除数据
        int row = permissionService.deleteByPrimaryKey(permissionId);
        if (row > 0){
            return MessageObject.createMo(1,"删除数据成功");
        }
        return MessageObject.createMo(0,"删除数据失败，请联系技术人员");
    }

    /**
     * @function 批量删除权限
     * @param permissionIds
     * @return
     */
    @ResponseBody
    @RequestMapping("/batchdel")
    @RequiresPermissions("permission:batchdel")
    public MessageObject permissionBatchDelete(String permissionIds){
        //1.删除数据前判断用户下面是否还有下属，如果有则不能删除
        String[] permissionIdsArr = permissionIds.split(",");
        for (String permissionId : permissionIdsArr) {
            PermissionExample example = new PermissionExample();
            PermissionExample.Criteria criteria = example.createCriteria();
            criteria.andParentIdEqualTo(Long.parseLong(permissionId));
            List<Permission> permissions = permissionService.selectByExample(example);
            if (permissions.size() > 0) {
                return MessageObject.createMo(0, "被删除的权限下还有子权限，不能删除");
            }
        }

        //2.根据permissionIdsArr删除数据
        int row = permissionService.deleteByIds(permissionIdsArr);
        if (row > 0){
            return MessageObject.createMo(1,"删除数据成功");
        }
        return MessageObject.createMo(0,"删除数据失败");
    }

    /**
     * @function 查询所有权限集合并返回给前端
     * @param m
     * @return
     */
    @ResponseBody
    @RequestMapping("/getAllPermissions")
//    @RequiresPermissions("permission:getAllPermissions")
    public List<Permission> permissionGetAllPermissions(Model m){
        System.out.println("hello");
        PermissionExample example = new PermissionExample();
        List<Permission> permissions = permissionService.selectByExample(example);
        return permissions;
    }

}
